package com.lk.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lk.model.EventMessage;
import com.lk.model.Person;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends Activity {
    private Button btn;
    private Button target;
    private TextView hello;
    private EditText myEt;
    private Button myBtn;
    private Button mvpBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
//        EventBus.getDefault().register(this);
        hello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!EventBus.getDefault().isRegistered(MainActivity.this))
                    EventBus.getDefault().register(MainActivity.this);
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isWxAvilible(MainActivity.this)) {
                    Intent i = new Intent();
                    ComponentName cName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
                    i.setAction(Intent.ACTION_MAIN);
                    i.setComponent(cName);
                    i.addCategory(Intent.CATEGORY_LAUNCHER);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
                } else {
                    Toast.makeText(MainActivity.this, "没有安装微信", Toast.LENGTH_SHORT).show();
                }
            }
        });
        target.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, TargetActivity.class));
            }
        });
        myBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Pattern p = Pattern.compile("^[\\u4e00-\\u9fa5a-zA-Z0-9]+$");
                Matcher matcher = p.matcher(myEt.getText().toString());
                if (matcher.matches()) {
                    Toast.makeText(MainActivity.this, "Yes", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Reject", Toast.LENGTH_LONG).show();
                }
            }
        });
        mvpBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MVPActivity.class));
            }
        });

//        testExecutor01();
//        testExecutor02();
        testExecutor03();
    }

    private void initView() {
        btn = (Button) findViewById(R.id.btn);
        target = (Button) findViewById(R.id.toTarget);
        hello = (TextView) findViewById(R.id.helloText);
        myEt = (EditText) findViewById(R.id.myEt);
        myBtn = (Button) findViewById(R.id.myBtn);
        mvpBtn = (Button) findViewById(R.id.testMvp);
    }

    public static boolean isWxAvilible(Context context) {
        PackageManager manager = context.getPackageManager();
        List<PackageInfo> pList = manager.getInstalledPackages(0);
        if (pList != null) {
            for (PackageInfo info : pList) {
                String pn = info.packageName;
                if ("com.tencent.mm".equals(pn))
                    return true;
            }
        }
        return false;
    }

    private void testExecutor01() {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
        for (int i = 1; i <= 10; i++) {
            final int index = i;
            fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    String threadName = Thread.currentThread().getName();
                    Log.e("zxy", "线程：" + threadName + ",正在执行第" + index + "个任务");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    private void testExecutor02() {
        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
        for (int i = 1; i <= 10; i++) {
            final int index = i;
            singleThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    String threadName = Thread.currentThread().getName();
                    Log.e("zxy", "线程" + threadName + "，正在执行第" + index + "个任务");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    private void testExecutor03() {
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
        //延迟2秒后执行该任务
        scheduledThreadPool.schedule(new Runnable() {
            @Override
            public void run() {
                String threadName = Thread.currentThread().getName();
                Log.e("zxy", "线程：" + threadName + ",正在执行");
            }
        }, 2, TimeUnit.SECONDS);
        //延迟1秒后，每隔2秒执行一次该任务
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {

            }
        }, 1, 2, TimeUnit.SECONDS);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEvent(EventMessage em) {
        if (em != null) {
            Person p = em.getP();
            hello.setText(p.getName() + "今年" + p.getAge() + "岁");
            Toast.makeText(this, p.getName() + "今年" + p.getAge() + "岁", Toast.LENGTH_SHORT).show();
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void handleEvent2(EventMessage em) {
        if (em != null) {
            Person p = em.getP();
            hello.setText(p.getName() + "今年" + p.getAge() + "岁");
            Toast.makeText(this, p.getName() + "今年" + p.getAge() + "岁", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
